@echo off

chcp 65001
REM by jackie lee @ 2021.10.11
REM such as: lib-src-copy.bat D:\Document\LeeSp\Stm32Pro\HALPro D:\Document\LeeSp\Stm32Pro\Template\HalTemplate
REM 创建模板项目  chcp修改编码为utf-8
REM 项目名称 %1

REM 当前目录
SET "BASE_DIR=%~dp0"
REM 项目名
SET "PROJECT_NAME=%~1"

echo %BASE_DIR%
echo %PROJECT_NAME%

REM 判断是否参数合法
if not defined PROJECT_NAME goto noproject

SET TEMPLATE_DIR=%BASE_DIR%\Template\HalTemplate
SET PROJECT_DIR=%BASE_DIR%\HALPro\%PROJECT_NAME%

if not exist %TEMPLATE_DIR% goto notemplate
if not exist %PROJECT_DIR% mkdir %PROJECT_DIR%

REM 复制模板内容
XCOPY /S /Y /EXCLUDE:create-hal-proj-ignore %TEMPLATE_DIR% %PROJECT_DIR%
CD %PROJECT_DIR%
REN Template.* %PROJECT_NAME%.*

REM replace
setlocal enabledelayedexpansion
SET PROJ_FILENAME=%PROJECT_NAME%.uvprojx
SET replaced=LayName^>Template
SET all=LayName^>%PROJECT_NAME%
SET replaced2=OutputName^>Template
SET all2=OutputName^>%PROJECT_NAME%

for /f "delims=" %%i in ('type "%PROJ_FILENAME%"') do (
   if "%%i"=="" (echo.) else (set "line=%%i" & call :chg)
)>> "%PROJ_FILENAME%"_tmp.txt

move "%PROJ_FILENAME%"_tmp.txt "%PROJ_FILENAME%"

echo %BASE_DIR%
REM CD /d %BASE_DIR% REM 无法切换
CD %~dp0 REM 为什么就是无法切出去呢 cd..也都不行

goto end

:noproject
echo input the smt32 hal project name for param 1
goto end

:notemplate
echo the create-hal-proj.bat not in the basic project dir
goto end

:failure
echo %LIB_DIR% not exists


:chg
set "line=!line:%replaced%=%all%!"
set "line=!line:%replaced2%=%all2%!"
echo !line!
goto :eof

:end

